package com.supermap.localization;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.apache.commons.lang3.StringUtils;

public class CheseSpellingDemo {

    public static void main(String[] args) throws IOException {
        Path fsample = Paths.get("E:", "supermap_work\\svn\\dev\\MonkeyKing\\modules\\cloudmgn-ui\\src\\main\\resources\\staticFiles\\js\\resources\\i18n",
                "test.txt");
        Path foutput = Paths.get("E:", "supermap_work/svn/FTL_Localization_Tool/release", "resourceNew.properties");

        updateSample(fsample, foutput);
    }

    private static void updateSample(Path samplePFile, Path foutput) throws IOException {
        BufferedReader br = Files.newBufferedReader(samplePFile, StandardCharsets.UTF_8);
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.startsWith("#") || line.equals("")) {// 忽略#开头的注释
                continue;
            }
            String tmp[] = StringUtils.split(line, "=");
            String msg = ChineseSpelling.getSpelling(tmp[1]);
            writeToEnd(foutput, tmp[0] + "=" + msg);
        }
        br.close();
    }

    private static void writeToEnd(Path fot, String msg) throws IOException {
        if (Files.notExists(fot)) {
            Files.createFile(fot);
        }
        BufferedWriter bw = Files.newBufferedWriter(fot, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
        bw.write(msg);
        bw.newLine();
        bw.close();
    }

}
